package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "用户管理")
@RequestMapping("/admin/system/sysUser")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    @Operation(summary = "条件查询用户")
    public Result<PageInfo<SysUser>> findByPage(SysUserDto sysUserDto,
                                                @PathVariable Integer pageNum,
                                                @PathVariable Integer pageSize){
        PageInfo<SysUser> pageInfo=sysUserService.findByPage(sysUserDto,pageNum,pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "保存用户")
    @PostMapping("/saveSysUser")
    public Result saveUser(@RequestBody SysUser sysUser){
        sysUserService.saveUser(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改用戶數據")
    @PutMapping("/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        sysUserService.updateSysUser(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("deleteById/{userId}")
    @Operation(summary = "删除用户")
    public Result removeById(@PathVariable("userId") Long id){
        sysUserService.removeById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
